const express = require('express');
const bodyParser = require('body-parser');
const FileHandler = require('./FileHandler');
const cors = require('cors');
const app = express();
const path = require('path')
const tools = require('./tools')
app.use(bodyParser.json({ limit: '50mb' })); // 设置为50MB  
app.use(bodyParser.urlencoded({ extended: true, limit: '50mb' }));

// 使用body-parser中间件解析POST请求体
app.use(bodyParser.json());
app.use(cors());

let dataAll = null
// POST请求的处理
app.post('/data', async (req, res) => {
	// 获取请求体中的数据
	const requestData = req.body;
	if (!dataAll) {
		dataAll = requestData;
	} else {
		dataAll.data.push(...requestData.data)
	}
	if (requestData.data.length != 50) {
		// console.log('requestData', requestData.data)
		// console.log('数据接收完毕', dataAll.data)
		// 准备xlsl 数据
		console.log(dataAll.data.length, '长度')
		const worksheetData = [

			['作者', '标题', '点赞数', '收藏数', '评论数', '内容详情'],
			...dataAll.data.map(item => {
				// console.log('item', item, item.userInfo)
				let arr = [
					item.userInfo.userName,
					item.title,
					item.likesNum,
					item.collectionNum,
					item.commentsNum,
					item.detail
				];
				return arr;
			})
		];
		// console.log('xlsl 数据', worksheetData);
		// console.log('资源 数据', worksheetData)
		// 这里简单地将接收到的数据返回给客户端w
		const fileHandler = new FileHandler('../data/' + requestData.catalogue + '/' + requestData.key)
		fileHandler.createXLSX(worksheetData);
		// for (let index = 0; index < dataAll.data.length; index++) {
		// 	let element = dataAll.data[index];
		// 	if (Array.isArray(element.files) && element.files.length) {
		// 		for (let index = 0; index < element.files.length; index++) {
		// 			const file = element.files[index];
		// 			console.log(file, 'file')
		// 			const fileUrl = file.fileUrl;
		// 			const fileName = file.fileName;
		// 			const dir = await fileHandler.createDir(element.title)
		// 			const outputFilePath = path.join(dir, fileName);
		// 			console.log('outputFilePath', outputFilePath)
		// 			try {
		// 				const result = await tools.downloadFile(fileUrl, outputFilePath);
		// 				console.log(result);
		// 			} catch (error) {
		// 				console.log('报错了', error)
		// 			}

		// 		}
		// 	}
		// }
	}

	// console.log('数据保存完毕准备 发送email')
	// let outPath = path.join('../data/', requestData.catalogue, '/zip' + requestData.key + '.zip')
	// console.log(outPath, 'outPath')
	// tools.compressAndDeleteFolder(fileHandler.directory, outPath, async (path) => {
	// 	try {
	// 		console.log('path', path)
	// 		await tools.sendEamil('2105873459@qq.com', requestData.key, path);
	// 		console.log('发送成功')
	// 	} catch (error) {
	// 		console.log('发送失败 error', error)
	// 	}
	// })
	// requestData.data.forEach(element => {
	//   console.log('element', element)
	//   fileHandler.readOrCreateFile((element.title || '无标题笔记') + '_' + element.id, element.detail, (type, data) => {
	//       console.log('writeFile')
	//   })
	// });

	res.json({ receivedData: requestData });
});

const PORT = 8090;
app.listen(PORT, () => {
	console.log(`Server running at http://localhost:${PORT}/`);
});


